package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;


import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.util.Format2Util;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;

import java.util.HashMap;
import java.util.List;

class KBarLayer extends BasicLayer
{
	public KBarLayer()
	{
		super();
	}
	
	@Override
	public Layer.ChartTypeEnum getLayerTypeEnum()
	{
		return Layer.ChartTypeEnum.KBAR;
	}

	@Override
	public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		List<CandleData> tDataList = m_callback.getCandleDataList();
		Coordinate tCoordinate = m_callback.getCoordinate();

		float tBeforeCloseFloat = 0;
		float tCloseFloat = 0;
		float tOpenFloat = 0;
		float tHighFloat = 0;
		float tLowFloat = 0;
		int tTimeInt = 0;
		float tValueFloat = 0;
		float tVolumnLong = 0;

		CandleData tTimeCandle = null;

		int tXPoint = pPoint.x;

		if (tDataList != null && tDataList.size() > 0)
		{
			int tListSize = tDataList.size();
			int tStartIndex = tCoordinate.getStartIndex();
			float tXFirstPosition = tCoordinate.getXFirstPosition();
			float tXInterval = tCoordinate.getXInterval();
			float tYFirstposition = tCoordinate.getLayerRectF().bottom;
			// float tYFirstposition = tCoordinate.getYFirstPosition();
			float tYMaxValue = tCoordinate.getMaxValue();
			float tYMinValue = tCoordinate.getMinValue();
			float tYValueDist = tYMaxValue - tYMinValue;
			float tYValueLength = tCoordinate.getLayerRectF().bottom - tCoordinate.getLayerRectF().top;
			// float tYValueLength = tCoordinate.getYInterval() *
			// tCoordinate.getYIntervalCount();

			float tBarWidth = Math.min(tXInterval / 3, 5);

			int tSysUpColor = m_callback.getSysUpColor();
			int tSysDownColor = m_callback.getSysDownColor();
			int tSysEvenColor = m_callback.getSysEvenColor();

			pPaint.setAntiAlias(true);
			pPaint.setStrokeWidth(2);

			for (int i = tStartIndex; i < tListSize; i++)
			{
				CandleData tCandleData;

				try
				{
					tCandleData = tDataList.get(i);
				}
				catch (Exception e)
				{
					continue;
				}

				if (tCandleData == null)
					continue;

				float tHigh = (float) tCandleData.getDbHigh();
				float tLow = (float) tCandleData.getDbLow();
				float tOpen = (float) tCandleData.getDbOpen();
				float tClose = (float) tCandleData.getDbClose();
				float tVolumn = tCandleData.m_lVolume;
								
				float tHighYPosition = tYFirstposition - ((tHigh - tYMinValue) * tYValueLength / tYValueDist);
				float tLowYPosition = tYFirstposition - ((tLow - tYMinValue) * tYValueLength / tYValueDist);
				float tOpenYPosition = tYFirstposition - ((tOpen - tYMinValue) * tYValueLength / tYValueDist);
				float tCloseYPosition = tYFirstposition - ((tClose - tYMinValue) * tYValueLength / tYValueDist);
				float tXPosition = tXFirstPosition + tXInterval * (i - tStartIndex);

				float tRectTopPosition = 0.f;
				float tRectBottomPosition = 0.f;

				if ((tXPosition - tXInterval / 2) <= pPoint.x && pPoint.x < (tXPosition + tXInterval / 2))
				{
					float tValueaa = (tYFirstposition - pPoint.y) * tYValueDist / tYValueLength + tYMinValue;
//					FDTLogUtils.d("tingtouch","tValueaa"+tValueaa);
					tValueFloat = tValueaa;

					tVolumnLong = tVolumn;
					tCloseFloat = tClose;
					tOpenFloat = tOpen;
					tHighFloat = tHigh;
					tLowFloat = tLow;
					tTimeCandle = tDataList.get(i);
					tXPoint = (int) tXPosition;

					try
					{
						if (i > 0)
						{
							float tBeforeClose = (float) tDataList.get(i - 1).getDbClose();
							tBeforeCloseFloat = tBeforeClose;
						}
					}
					catch (Exception e)
					{
					}
				}
				
				if (tOpen > tClose)
				{
					pPaint.setColor(tSysDownColor);
					tRectTopPosition = tOpenYPosition;
					tRectBottomPosition = tCloseYPosition;
				}
				else if (tOpen == tClose)
				{
					pPaint.setColor(tSysEvenColor);
					tRectTopPosition = tCloseYPosition;
					tRectBottomPosition = tOpenYPosition;
				}
				else
				{
					pPaint.setColor(tSysUpColor);
					tRectTopPosition = tCloseYPosition;
					tRectBottomPosition = tOpenYPosition;
				}

				
				if (tRectTopPosition == tRectBottomPosition)
				{
					pCanvas.drawLine(tXPosition - tBarWidth, tRectTopPosition, tXPosition + tBarWidth, tRectBottomPosition, pPaint);
				}
				else
				{
					pCanvas.drawRect(tXPosition - tBarWidth, tRectTopPosition, tXPosition + tBarWidth, tRectBottomPosition, pPaint);
				}

				pCanvas.drawLine(tXPosition, tRectTopPosition, tXPosition, tHighYPosition, pPaint);
				pCanvas.drawLine(tXPosition, tRectBottomPosition, tXPosition, tLowYPosition, pPaint);

			}
		}

		RectF tLayerRect = tCoordinate.getLayerRectF();

//		FDTLogUtils.d("tingtouch","setCrossData tTimeText"+tTimeInt+"tValueText"+tValueFloat);

		int digit = m_callback.getDigit();

		String time = null;
		String tValueText = FormatUtil.getFormatString(tValueFloat, digit);


		if (tTimeCandle != null)
		{
			time = getDisplayX(m_callback.getChartType(), tTimeCandle);
		}

		setCrossData(pCanvas, pPaint, time, tValueText, new Point(tXPoint, pPoint.y));

		if (tLayerRect.contains(tXPoint, pPoint.y))
		{
			HashMap<String, String> tMap = new HashMap<String, String>();
			tMap.put("Close", tCloseFloat == 0 ? "" : FormatUtil.getFormatString(tCloseFloat, digit));
			tMap.put("Open", tOpenFloat == 0 ? "" : FormatUtil.getFormatString(tOpenFloat, digit));
			tMap.put("High", tHighFloat == 0 ? "" : FormatUtil.getFormatString(tHighFloat, digit));
			tMap.put("Low", tLowFloat == 0 ? "" : FormatUtil.getFormatString(tLowFloat, digit));
			tMap.put("BeforeClose", tBeforeCloseFloat == 0 ? "" : FormatUtil.getFormatString(tBeforeCloseFloat, digit));
			tMap.put("Volumn", tVolumnLong == 0 ? "" : Format2Util.doubleToHumanReadableFormat(tVolumnLong, 2));
			m_callback.pointFeedback(tMap,pPoint );
		}
	}
}
